<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色管理编辑</title>
<link href="/resources/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="/resources/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="/resources/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/resources/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/resources/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="/resources/lib/json2.js" type="text/javascript"></script>
<script type="text/javascript">
var form;
var manager;
var list = '${list}';
var data = '${data}';
var snumber = '${sn}';//流水号
var TreeData = JSON.parse(list); 
$(function (){
    //工具条
    $("#toptoolbar").ligerToolBar({ items: [
        { text: '保存', id:'save',click: submitForm,icon: 'save' }
    ]
    });
	//创建form对象
	 form = $("#form").ligerForm({
    	  inputWidth: 150, labelWidth: 80, space: 40,
          validate: true,
          fields: [ 
              { name: "uuid", type: "hidden" },
              { label: '角色编号', name: 'roleNum',newline: true,type:"text",validate: {required: true,minlength: 1 }},
              { name: "resourcesIds", type: "hidden" },
              { label: "角色名", name: "roleName", newline: true, type: "text", validate: {required: true,minlength: 1 } },
              { label: "备注", name: "remark", newline: true, type: "text" },
          ]
    	});
	
		//树形
		manager = $("#tree1").ligerTree({
			 data:TreeData,
			 checkbox: true,
			 isExpand: 2,
	         idFieldName :'uuid',
	         slide:false,
	         parentIDFieldName:'parentId',
	         textFieldName:'resourceName',
	     });
		if(data != null && data != ''){
			var objectData = JSON.parse(data);
			form.setData(objectData);
		}
		if(snumber&&typeof(snumber.length)!='undefined'){
			form.setData({roleNum:snumber});
		}
		
		if('${status}' == 1){
			$('#toptoolbar').hide();
		}
	});
	
	//提交
	function submitForm(){
		//获取选择数据
		var nodes = manager.getChecked();
		var s = '';
		for(var i=0; i<nodes.length; i++){
			if (s != '') s += ',';
			s += nodes[i].data.uuid;
		}
		var ids = {"resourcesIds":s};
		form.setData(ids);
		var data = form.getData(); 
		if (form.valid()){
			console.log(data);
			$.ajax({
            loading: '正在保存数据中...',
            type:'post',
 		    dataType:'json',
            url: "/sysRole/saveOrUpdateSysRole",
            data: data,
            success: function (data) {
            	if(data.code == 'FAILE'){
            		$.ligerDialog.error(data.message);
            	}else{
            		$.ligerDialog.success(data.message,'信息',function(){
    					form.setData({"uuid":data.data});
    				});
            	}
            },
            error: function (message) {
           	 	$.ligerDialog.error("保存失败");
            }
        });
     }else{
         form.showInvalid();
     } 
	}
	
</script>
</head>
<body>
	<div id="toptoolbar"></div> 
	<form id="form"></form>
	<div style="float: left;width: 30%" >
        <h4>菜单树</h4>
        <div class="tree"> 
            <ul id="tree1"></ul> 
        </div> 
    </div>
	
    <div style="display:none"></div>
</body>
</html>